<?php
class cron_imgControl extends skymvc{
	
	public function __construct(){
		parent::__construct();	
	}
	
	public function onDefault(){
		set_time_limit(120);
		$key="index_cron_img_default";
		if(!cache()->get($key)){
			cache()->set($key,1,5);
			$data=M("cron_img")->select(array(
				"where"=>" status=0 ",
				"order"=>" id ASC",
				"limit"=>10,
			));
			if($data){
				foreach($data as $k=>$v){
					$ids[]=$v['id'];
				}
				M("cron_img")->update(array(
					"status"=>1
				),"id in("._implode($ids).") ");
				
				foreach($data as $k=>$v){
					$user_head=$v['imgurl'];
					if($v['fromimg']){
						$imcontent=curl_get_contents($v['fromimg']);
						if(empty($imcontent)) exit('获取远程图片失败');
						umkdir(dirname($v['imgurl']));
						file_put_contents($v['imgurl'],$imcontent);
						$im=getimagesize($user_head);
						if($im[0]==0){
							M("cron_img")->update(array(
								"status"=>2
							),"id=".$v['id']);
							continue;
						}
					}
					
					$this->loadClass("image",false,false);
					$img=new image();
					$img->makethumb($user_head.".100x100.jpg",$user_head,"100","100",1);
					$img->makethumb($user_head.".small.jpg",$user_head,"240");
					$img->makethumb($user_head.".middle.jpg",$user_head,"440");
					M("cron_img")->delete("id=".$v['id']);
				}
			
				echo "success";
			}else{
				echo "empty";
			}
			
		}
		
	}
	
	
	public function onT(){
		$v=M("cron_img")->selectRow(array(
				"where"=>" status=1 ",
				"order"=>" id ASC",
				"limit"=>1,
		));
		$user_head=$v['imgurl'];
		if($v['fromimg']){
			$st=microtime(true);
			$imcontent=curl_get_contents($v['fromimg']);
			$ed=microtime(true);
			umkdir(dirname($v['imgurl']));
			file_put_contents($v['imgurl'],$imcontent);
			$im=getimagesize($user_head);
		 	print_r($im);
			if($im[0]==0){
				M("cron_img")->update(array(
					"status"=>2
				),"id=".$v['id']);
				continue;
			}
		
		
		$this->loadClass("image",false,false);
		$img=new image();
		$img->makethumb($user_head.".100x100.jpg",$user_head,"100","100",1);
		$img->makethumb($user_head.".small.jpg",$user_head,"240");
		$img->makethumb($user_head.".middle.jpg",$user_head,"440");
		M("cron_img")->delete("id=".$v['id']);
		}
		if($v){
			M("cron_img")->update(array(
					"status"=>2
				),"id=".$v['id']);
		}
		echo "生成成功 ".$v['id']."||".$v['imgurl']."花费时间：".($ed-$st);
		echo "<script>setTimeout(function(){window.location.reload()},1000)</script>";
	}
	
}

?>